﻿using NewLife.Remoting;

namespace newlife.rpc.server.console
{
    /// <summary>
    /// 设备服务（服务端功能的实现）
    /// 备注：调用时要忽略 Service 后缀
    /// </summary>
    public class DeviceService : IApi
    {
        public IApiSession Session { get; set; } = null!;

        /// <summary>
        /// 上报设备温度
        /// </summary>
        /// <param name="temperature">设备的温度</param>
        /// <param name="deviceId">设备的编号</param>
        /// <returns>上报成功返回 true，反之返回 false</returns>
        public bool ReportTemperature(float temperature, string deviceId)
        {
            Console.WriteLine($"收到设备 {deviceId} 上报的温度：{temperature}℃");
            return true;
        }

        /// <summary>
        /// 获取设备采样频率
        /// </summary>
        /// <param name="deviceId">设备的编号</param>
        /// <returns>采样频率</returns>
        public int GetSampleRate(string deviceId)
        {
            Task.Run(() => 
            {
                Session.InvokeOneWay("notice", new { deviceId, msg = "服务端主动通知" });
            });

            return deviceId == "D001" ? 5 : 10;
        }
    }
}
